package com.centaline.androidsalesblog.widgets.dropdown;

import android.content.Context;
import android.support.percent.PercentRelativeLayout;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.businissobject.DropBo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 更多
 * Created by guilin on 16/1/27.
 */
public class MoreDrop extends AbsDrop {

    private PercentRelativeLayout prl_parent;
    private ListView lv_left, lv_right;
    private Button btn_reset, btn_confirm;
    private DropTextAdapter leftAdapter;
    private DropCheckAdapter rightAdapter;
    private HashMap<Integer, SparseBooleanArray> hashMap = new HashMap<>();
    private SparseIntArray sparseIntArray = new SparseIntArray();

    public MoreDrop(View anchor, Context context) {
        super(anchor, context);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_drop_more, null);
        prl_parent = (PercentRelativeLayout) view.findViewById(R.id.prl_parent);
        prl_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        lv_left = (ListView) view.findViewById(R.id.lv_left);
        lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                leftAdapter.select(position);
                if (arrayList.get(position).isSelected()) {
                    if (!hashMap.containsKey(position)) {
                        hashMap.put(position, new SparseBooleanArray());
                    }
                    rightAdapter.setData(arrayList.get(position).getChildrenList(), hashMap.get(position));
                } else {
                    rightAdapter.setData(arrayList.get(position).getChildrenList(), sparseIntArray.get(position, 0));
                    lv_right.smoothScrollToPosition(sparseIntArray.get(position, 0));
                }
            }
        });
        lv_right = (ListView) view.findViewById(R.id.lv_right);
        lv_right.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (arrayList.get(leftAdapter.getSelectedPos()).isSelected()) {
                    rightAdapter.select(position);
                } else {
                    sparseIntArray.put(leftAdapter.getSelectedPos(), position);
                    rightAdapter.setSelectPos(position);
                }
            }
        });
        btn_reset = (Button) view.findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hashMap.clear();
                sparseIntArray.clear();
                leftAdapter.select(0);
                if (arrayList.get(0).isSelected()) {
                    hashMap.put(0, new SparseBooleanArray());
                    rightAdapter.setData(arrayList.get(0).getChildrenList(), hashMap.get(0));
                } else {
                    rightAdapter.setData(arrayList.get(0).getChildrenList(), sparseIntArray.get(0, 0));
                    lv_right.smoothScrollToPosition(sparseIntArray.get(0, 0));
                }
            }
        });
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ArrayList<DropBo> result = new ArrayList<>();
                final int intSize = sparseIntArray.size();
                for (int i = 0; i < intSize; i++) {
                    result.add(arrayList.get(
                            sparseIntArray.keyAt(i)).getChildrenList().get(sparseIntArray.valueAt(i)));
                }
                if (hashMap.size() > 0) {
                    Iterator<Map.Entry<Integer, SparseBooleanArray>> iterator = hashMap.entrySet().iterator();
                    while (iterator.hasNext()) {
                        final Map.Entry<Integer, SparseBooleanArray> entry = iterator.next();
                        final int boolSize = entry.getValue().size();
                        for (int j = 0; j < boolSize; j++) {
                            if (entry.getValue().valueAt(j)) {
                                result.add(arrayList.get(entry.getKey()).getChildrenList().get(entry.getValue().keyAt(j)));
                            }
                        }
                    }
                }

                final int resultSize = result.size();
                if (resultSize == 0) {
                    dropComplete(true, 0);
                } else {
                    dropComplete(true, 0, result.toArray(new DropBo[resultSize]));
                }
            }
        });
        leftAdapter = new DropTextAdapter();
        rightAdapter = new DropCheckAdapter();
        lv_left.setAdapter(leftAdapter);
        lv_right.setAdapter(rightAdapter);
        initPopWindow(view);
    }

    @Override
    public void init(ArrayList<DropBo> dropBos) {
        arrayList.clear();
        arrayList.addAll(dropBos);
        hashMap.clear();
        leftAdapter.setArrayList(arrayList);
        if (arrayList.size() > 0) {
            if (arrayList.get(0).isSelected()) {
                hashMap.put(0, new SparseBooleanArray());
                rightAdapter.setData(arrayList.get(0).getChildrenList(), hashMap.get(0));
            } else {
                rightAdapter.setData(arrayList.get(0).getChildrenList(), sparseIntArray.get(0, 0));
                lv_right.smoothScrollToPosition(sparseIntArray.get(0, 0));
            }
        }
    }

    @Override
    public void onDismiss() {

    }
}
